<?php
defined('_JEXEC') or die('Restricted access');

class ScorecardPager {
	var $page = null;
	var $pagesize = null;
	var $total = null;
	var $action = null;
	var $pagecount = null;
	var $formname = null;
	var $showcount = null;

	function __construct($params = null) {
		$this->page = isset($params['page']) ? $params['page'] : 1;
		$this->total = isset($params['total']) ? $params['total'] : 0;
		$this->pagesize = isset($params['pagesize']) ? $params['pagesize'] : 10;
		$this->action = isset($params['action']) ? $params['action'] : '';
		$this->formname = isset($params['formname']) ? $params['formname'] : 'form';
		$this->showcount = isset($params['showcount']) ? $params['showcount'] : 5;
		$this->pagecount = ceil($this->total / $this->pagesize);
	}

	function render() {
		if ($this->pagecount <= 0) return;

		$str =  '<script language="javascript" type="text/javascript">';
		$str .= '  		function doPagerSubmit(action,page) {';
		$str .= '			var form = document.forms["scoreform"];';
		$str .= '			form.pagerpage.value = page;';
		$str .= '			form.task.value = action;';
		$str .= '			form.submit();';
		$str .= '  		}';
		$str .= '</script>';
		$str .= '<input type="hidden" name="pagerpage" id="pagerpage" value=""/>';

		$str .= '<ul class="pager">';
		
		$start = ceil($this->page / $this->showcount);
		if ($start > 1) $start = ($start - 1) * $this->showcount + 1;
		
		$end = min($this->pagecount, $start + $this->showcount - 1);
			
		if ($start > 1) {
			$str .= '<li>';
			$str .= " <a href=\"#\" onclick=\"doPagerSubmit('" . $this->action . "', '" . 1 . "');\">" . '<<' . "</a>";
			$str .= '</li>';
	
			$str .= '<li>';
			$str .= " <a href=\"#\" onclick=\"doPagerSubmit('" . $this->action . "', '" . ($this->page > 1 ? $this->page -1 : $this->page) . "');\">" . '<' . "</a>";
			$str .= '</li>';
		}
		
		for ($i = $start; $i <= $end; $i++) {
			$class = ($this->page == $i) ? 'selected' : '';
			$str .= '<li>';
			$str .= " <a href=\"#\" class=\"$class\" onclick=\"doPagerSubmit('" . $this->action . "', '" . $i . "');\">" . $i . "</a>";
			$str .= '</li>';
		}
		
		if ($end < $this->pagecount) {
			$str .= '<li>';
			$str .= " <a href=\"#\" onclick=\"doPagerSubmit('" . $this->action . "', '" . ($this->page < $this->pagecount ? $this->page + 1 : $this->page) . "');\">" . '>' . "</a>";
			$str .= '</li>';
			
			$str .= '<li>';
			$str .= " <a href=\"#\" onclick=\"doPagerSubmit('" . $this->action . "', '" . $this->pagecount . "');\">" . '>>' . "</a>";
			$str .= '</li>';
		}
		$str .= '</ul>';
		return $str;
	}
}
?>
